package com.bei_en.teacher.management.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import com.bei_en.common.spring.DateEditor;
import com.bei_en.common.spring.DoubleEditor;
import com.bei_en.common.spring.IntegerEditor;
import com.bei_en.common.spring.LongEditor;
import com.bei_en.common.util.Mylog;
import com.bei_en.teacher.admin.model.education.bean.Education;
import com.bei_en.teacher.admin.model.education.condition.EducationCondition;
import com.bei_en.teacher.admin.model.education.vo.EducationVo;
import com.bei_en.teacher.admin.model.questions.condition.QuestionsTypeCondition;
import com.bei_en.teacher.admin.model.questions.vo.QuestionsTypeVo;
import com.bei_en.teacher.admin.model.regions.condition.RegionsCondition;
import com.bei_en.teacher.admin.model.regions.vo.RegionsVo;
import com.bei_en.teacher.admin.model.subject.condition.SubjectCondition;
import com.bei_en.teacher.admin.model.subject.vo.SubjectVo;
import com.bei_en.teacher.admin.service.education.EducationService;
import com.bei_en.teacher.admin.service.questions.QuestionsTypeService;
import com.bei_en.teacher.admin.service.regions.RegionsService;
import com.bei_en.teacher.admin.service.subject.SubjectService;
import com.bei_en.teacher.management.model.system.condition.SystemRoleCondition;
import com.bei_en.teacher.management.model.system.condition.SystemUserCondition;
import com.bei_en.teacher.management.model.system.vo.SystemLogsVo;
import com.bei_en.teacher.management.model.system.vo.SystemUserVo;
import com.bei_en.teacher.management.other.Consts;
import com.bei_en.teacher.management.service.system.SystemLogsService;
import com.bei_en.teacher.management.service.system.SystemRoleService;
import com.bei_en.teacher.management.service.system.SystemUserService;
import com.bei_en.teacher.management.web.MyJsonResult;
import com.google.gson.Gson;

public abstract class BaseController {

	protected Logger log = Logger.getLogger(this.getClass());

	public static String net = "http://219.159.73.239:8080/img/";

	// protected WpLogger wplog = Logger.getLogger(this.getClass());
	public static Gson gson = new Gson();
	@Autowired
	protected HttpServletRequest request;
	@Autowired
	protected HttpSession session;
	@Autowired
	protected SystemLogsService systemLogsService;
	@Autowired
	protected SystemUserService systemUserService;
	@Autowired
	protected SystemRoleService systemRoleService;
	@Autowired
	QuestionsTypeService questionsTypeService;
	@Autowired
	SubjectService subjectService;
	@Autowired
	EducationService educationService;
	@Autowired
	RegionsService regionsService;

	// @Autowired
	// protected HttpServletResponse response;
	/*
	 * @Autowired private ResourceBundleMessageSource _res;
	 */

	// @Autowired
	// protected SpringContextHolder _contextHolder;

	@InitBinder
	protected void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) throws Exception {

		binder.registerCustomEditor(int.class, new IntegerEditor());
		binder.registerCustomEditor(long.class, new LongEditor());
		binder.registerCustomEditor(double.class, new DoubleEditor());
		binder.registerCustomEditor(Date.class, new DateEditor());
	}

	public HttpServletRequest getRequest() {

		return request;
	}

	/*
	 * @ExceptionHandler(Exception.class) public String exception(Exception e,
	 * HttpServletRequest request, HttpServletResponse response, String msg) {
	 * 
	 * e.printStackTrace(); log.warn(msg); request.setAttribute("exception", e);
	 * 
	 * if (ServerInfo.isAjax(request)||request.getParameter("ajax")!=null) {
	 * writeDwzSignal("300", msg, "", "", null, response);
	 * 
	 * return null; }
	 * 
	 * // ModelAndView mav = new ModelAndView("error.html"); //
	 * mav.addObject("statusCode", 300); // mav.addObject("message", msg);
	 * request.setAttribute("statusCode", 300); request.setAttribute("message",
	 * msg);
	 * 
	 * return "error"; }
	 */

	public SystemUserVo getLoginUser(HttpSession session) {

		Object obj;
		obj = SecurityUtils.getSubject().getSession()
				.getAttribute(Consts.LOGIN_USER_ADMIN);
		if (obj == null)
			return null;
		return (SystemUserVo) obj;
	}

	protected void writeJson(HttpServletResponse resp, MyJsonResult re) {

		try {
			resp.getWriter().print(gson.toJson(re));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void syslog(SystemLogsVo vo) {

		systemLogsService.insert(vo);
	}

	public void logsInsert(Integer userId, String content, Integer type) {

		HttpServletRequest request = getRequest();
		SystemLogsVo vo = new SystemLogsVo();
		vo.setUserId(userId);
		vo.setContent(content);
		vo.setType(type);
		vo.setIp(Mylog.getIpAddr(request));
		vo.setCreateTime(new Date());
		systemLogsService.insert(vo);
	}

	public Map<String, String> systemUserMap() {

		SystemUserCondition vo = new SystemUserCondition();
		vo.setPageSize(Integer.MAX_VALUE);
		return systemUserService.findSysUserPageMap2(vo);
	}

	public Map<String, String> roleMap() {

		SystemRoleCondition vo = new SystemRoleCondition();
		vo.setPageSize(Integer.MAX_VALUE);
		return systemRoleService.findSystemRolePageMap(vo);
	}

	public Map<String, String> getListChapter() {
		QuestionsTypeCondition questionsTypeCondition = new QuestionsTypeCondition();
		questionsTypeCondition.setPageSize(Integer.MAX_VALUE);
		List<QuestionsTypeVo> questionsTypeVos = questionsTypeService
				.findQuestionsTypePage(questionsTypeCondition);
		Map<String, String> list = new HashMap<String, String>();
		QuestionsTypeVo questionsTypeVo = null;
		for (int i = 0; i < questionsTypeVos.size(); i++) {
			questionsTypeVo = questionsTypeVos.get(i);
			list.put(questionsTypeVo.getId() + "", questionsTypeVo.getName());
		}
		return list;
	}

	public Map<String, String> getListSubject() {

		//
		SubjectCondition questionsTypeCondition = new SubjectCondition();
		questionsTypeCondition.setPageSize(Integer.MAX_VALUE);
		List<SubjectVo> questionsTypeVos = subjectService
				.findSubjectPage(questionsTypeCondition);
		Map<String, String> list = new HashMap<String, String>();
		SubjectVo questionsTypeVo = null;
		for (int i = 0; i < questionsTypeVos.size(); i++) {
			questionsTypeVo = questionsTypeVos.get(i);
			list.put(questionsTypeVo.getId() + "", questionsTypeVo.getName());
		}
		return list;
	}

	public Map<String, String> getListEducation() {

		//
		EducationCondition questionsTypeCondition = new EducationCondition();
		questionsTypeCondition.setPageSize(Integer.MAX_VALUE);
		List<EducationVo> questionsTypeVos = educationService
				.findEducationPage(questionsTypeCondition);
		Map<String, String> list = new HashMap<String, String>();
		EducationVo questionsTypeVo = null;
		for (int i = 0; i < questionsTypeVos.size(); i++) {
			questionsTypeVo = questionsTypeVos.get(i);
			list.put(questionsTypeVo.getId() + "", questionsTypeVo.getName());
		}
		return list;
	}

	public Map<String, String> getProvince() {
		RegionsCondition Condition = new RegionsCondition();
		Condition.setPageSize(Integer.MAX_VALUE);
		Condition.setAreaId(0);
		Condition.setCityId(0);
		Condition.setProvinceId(0);
		List<RegionsVo> listS = regionsService
				.findRegionsPage(Condition);
		Map<String, String> list = new HashMap<String, String>();
		RegionsVo Vo = null;
		for (int i = 0; i < listS.size(); i++) {
			Vo = listS.get(i);
			list.put(Vo.getItemId() + "", Vo.getItemName());
		}
		return list;
	}
	public Map<String, String> getCity() {
		RegionsCondition Condition = new RegionsCondition();
		Condition.setPageSize(Integer.MAX_VALUE);
		Condition.setAreaId(0);
		Condition.setQueryWhere(" and city_id!=0 and province_id!=0");
		List<RegionsVo> listS = regionsService
				.findRegionsPage(Condition);
		Map<String, String> list = new HashMap<String, String>();
		RegionsVo Vo = null;
		for (int i = 0; i < listS.size(); i++) {
			Vo = listS.get(i);
 			 switch (Vo.getProvinceId()) {
 			
				case 0:

					list.put(Vo.getCityId() + "", Vo.getItemName());
				break;

			default:
				list.put(Vo.getCityId() + "", "统考");
				break;
			}
		}
		return list;
	}
	public Map<String, String> getArea() {
		RegionsCondition Condition = new RegionsCondition();
		Condition.setPageSize(Integer.MAX_VALUE);
		Condition.setQueryWhere(" and city_id!=0 and area_id!=0 and province_id!=0");
		List<RegionsVo> listS = regionsService
				.findRegionsPage(Condition);
		Map<String, String> list = new HashMap<String, String>();
		RegionsVo Vo = null;
		for (int i = 0; i < listS.size(); i++) {
			Vo = listS.get(i);
			list.put(Vo.getAreaId() + "", Vo.getItemName());
		}
		return list;
	}
	
	   public JSONArray getRegionsArea(RegionsCondition vo){
     		List<RegionsVo> list =new ArrayList<RegionsVo>();
  			vo.setQueryWhere("and area_id!=0");
  			vo.setPageSize(Integer.MAX_VALUE);
  		   list = regionsService.findRegionsPage(vo);
  		   JSONObject object=null;
  		   JSONArray array=new JSONArray();
  		   for (int i = 0; i < list.size(); i++) {
  			   object=new JSONObject();
  			   object.accumulate("value", list.get(i).getAreaId());
  			   object.accumulate("label", list.get(i).getItemName());
  			   array.add(object);
  		}
  		  return array;
      }
      public JSONArray getRegionsCity(RegionsCondition vo){
     		List<RegionsVo> list =new ArrayList<RegionsVo>();
      		vo.setPageSize(Integer.MAX_VALUE);
      		   list = regionsService.findRegionsPage(vo);
      		   JSONObject object=null;
      		   JSONArray array=new JSONArray();
     			 switch ( vo.getProvinceId()) {
   				case 0:
   				   for (int i = 0; i < list.size(); i++) {
		      			   object=new JSONObject();
		   
		      			   object.accumulate("value", list.get(i).getCityId());
		      			  object.accumulate("label", list.get(i).getItemName());
		      			   array.add(object);
		      		}
 					break;
 				default:
 			
 					  object=new JSONObject();
					   object.accumulate("label","统考");
					   object.accumulate("value", list.get(0).getCityId());
					   array.add(object);
 					break;
 				}
      		   
      		
      		   return array;
      }
}
